import { reqGoodsInfo, reqAddOrUpdateShopCart } from "@/api";
// 封装的是游客身份模块uuid--生成一个堆积字符串
import {getUUID} from '@/utils/uuid_token.js'

const state = {
  goodsDetail: {},
  // 游客的临时身份
  uuid_token:getUUID()
};
const mutations = {
  GETGOODS(state, goodsDetail) {
    state.goodsDetail = goodsDetail;
  },
};
const actions = {
  // 获取商品详情
  async getGoodsInfo({ commit }, skuId) {
    let res = await reqGoodsInfo(skuId);
    // console.log(res)
    if (res.code === 200) {
      commit("GETGOODS", res.data);
    }
  },
  // 产品添加到购物车中
  async addOrUpdateShopCart({ commit }, { skuId, skuNum }) {
    let res = await reqAddOrUpdateShopCart(skuId, skuNum);
    if (res.code == 200) {
      //  返回成功的标记
      return "ok";
    } else {
      return Promise.reject(new Error("faile"));
    }
  },
};
const getters = {
  
  catagoryView(state) {
    return state.goodsDetail.categoryView || {};
    //   没数据的时候返回一个空对象
  },
  skuInfo(state) {
    return state.goodsDetail.skuInfo || {};
  },
  spuSaleAttrList(state) {
    return state.goodsDetail.spuSaleAttrList || {};
  },
};

export default {
  state,
  mutations,
  actions,
  getters,
};
